<template>
  <view class="home_create">
    <uni-list>
      <uni-list-item title="门店名称">
        <template slot="footer">
          <input v-model:value="storeName" placeholder="请输入门店名称" style="text-align:right;font-size: 32rpx;" />
        </template>
      </uni-list-item>
      <uni-list-item title="门店位置" link :rightText="storeAddress!=''?storeAddress:'设定位置'" @tap="gotolay"></uni-list-item>
    </uni-list>
    <view class="btn_baocun">
      <button type="primary" @tap="savageStore" style="width: 80%;height: 80rpx;line-height: 80rpx;">保存</button>
    </view>
  </view>
</template>

<script>
  import {
    addStore
  } from '@/api/system/store/store.js'
  export default {
    data() {
      return {
        storeAddress: "",
        storeName: "",
        userId: uni.getStorageSync("userId"),
        latitude: "",
        longitude: "",
      }
    },
    methods: {
      //设定位置方法
      gotolay() {
        //弹窗定位让其选择，成功后把地址和名称一起赋值给地址字段
        uni.chooseLocation({
          success: res => {
            this.storeAddress = res.name
            this.latitude = res.latitude
            this.longitude = res.longitude
          },
          fail: err => {
            this.$modal.msgError(err);
          }
        })
      },
      //新增一个门店方法
      savageStore() {
        if (this.storeName == "") {
          this.$modal.msgError("名称不能为空");
        } else {
          const data = {
            storeName: this.storeName,
            storeAddress: this.storeAddress,
            lat: this.latitude,
            lon: this.longitude
          }
          addStore(data).then(response => {
            if (response.code == 200) {
              uni.navigateBack({
                delta: 1,
                success: (res) => {
                  if (res.errMsg = "navigateBack:ok") {
                    this.$modal.msgSuccess("新增门店成功");
                  }
                },
              })
            } else {
              this.$modal.msgError("新增门店失败");
            }
          })
        }
      }
    }
  }
</script>

<style>
  .home_create {
    width: 92%;
    margin: 0 auto;
  }

  ::v-deep .home_create .uni-list-item__extra-text {
    font-size: 32rpx;
    overflow: hidden;
    text-align: right;
    vertical-align: middle;
    width: 300rpx;
  }

  .btn_baocun {
    margin-top: 40rpx;
  }
</style>